package worldinruin.gui.imp.gameview;


import java.awt.Dimension;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JLabel;
import javax.swing.border.EtchedBorder;

import worldinruin.core.IPlayer;
import worldinruin.core.IResources;
import worldinruin.core.imp.ResourceTypes;
import worldinruin.resources.IconMap;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ResourcePanel extends javax.swing.JPanel implements PropertyChangeListener {
	private JLabel woodIcon;
	private JLabel foodIcon;
	private JLabel ironIcon;
	private JLabel stoneIcon;
	private IPlayer player;
	
	/**
	 * Creates a resource panel with given player's resources.
	 * @param player Player who has resources.
	 */
	public ResourcePanel(IPlayer player) {
		super();
		this.player = player;
		player.addPropertyChangeListener(this);
		initGUI();
		setupGUI(player.getResources());
	}
	
	/**
	 * Updates the resource panel.
	 */
	public void update() {
		update(player.getResources());
	}
	
	/**
	 * Updates the resource panel.
	 * @param r New values from resources is updated.
	 */
	public void update(IResources r){
		woodIcon.setText(r.getWood() + "");
		stoneIcon.setText(r.getStone() + "");
		ironIcon.setText(r.getIron() + "");
		foodIcon.setText(r.getWood() + "");
	}
	
	/**
	 * Sets the GUI up with resource values.
	 * @param r Resource values.
	 */
	public void setupGUI(IResources r){
		woodIcon.setIcon(IconMap.getInstance().getScaledIcon(IconMap.getInstance().getIcon(ResourceTypes.WOOD), 50, 50));
		stoneIcon.setIcon(IconMap.getInstance().getScaledIcon(IconMap.getInstance().getIcon(ResourceTypes.STONE), 50, 50));
		ironIcon.setIcon(IconMap.getInstance().getScaledIcon(IconMap.getInstance().getIcon(ResourceTypes.IRON), 50, 50));
		foodIcon.setIcon(IconMap.getInstance().getScaledIcon(IconMap.getInstance().getIcon(ResourceTypes.FOOD), 50, 50));
		
		update(r);
		
		setBorder(new EtchedBorder());
	}
	
	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		try {
			setPreferredSize(new Dimension(500, 50));
			GridLayout thisLayout = new GridLayout(1, 1);
			thisLayout.setHgap(5);
			thisLayout.setVgap(5);
			thisLayout.setColumns(1);
			this.setLayout(thisLayout);
			{
				woodIcon = new JLabel();
				this.add(woodIcon);
			}
			{
				stoneIcon = new JLabel();
				this.add(stoneIcon);
			}
			{
				ironIcon = new JLabel();
				this.add(ironIcon);
			}
			{
				foodIcon = new JLabel();
				this.add(foodIcon);
								
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		update();
	}
}
